package com.haier.controller.manager;

import com.haier.constant.HpConstant;
import com.haier.utils.GetUtils;
import com.haier.utils.Servlets;
import com.haier.utils.XlsxExcelCreation;
import com.haier.vo.HpGoApplyVo;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.haier.dto.HpGoApplyDto;
import com.haier.service.IHpGoApplyService;
import com.haier.vo.ResponseVo;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 申请
 */
@RestController
@RequestMapping(value = "/hipassjava/manager/goApply")
public class ManagerHpGoApplyContoller {
	private static final Logger logger = LoggerFactory.getLogger(ManagerHpGoApplyContoller.class);
	@Autowired
	private IHpGoApplyService iHpGoApplyService;

	/**
	 * 查询分页
	 */
	@RequestMapping(value = "/page", method = RequestMethod.POST)
	public Object page(@RequestBody HpGoApplyDto dto) {
		try {
			return ResponseVo.success(iHpGoApplyService.pageByH5(dto));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

	/**
	 * 导出 未用
	 * @throws IOException
	 */
	//@RequestMapping(value = "/reportApply", method = RequestMethod.GET)
	public void reportApply(HttpServletResponse response, HpGoApplyDto dto) throws IOException {
 		String filename = GetUtils.getDateString(new Date(), "yyyyMMddHHmmss")
				+ URLEncoder.encode("申请报表.xlsx", "UTF-8");
		response.setContentType("application/octet-stream; charset=UTF-8");
		response.setHeader("Content-Disposition", "attachment; filename=" + filename);
		SXSSFWorkbook book = iHpGoApplyService.reportApply(dto);
		book.write(response.getOutputStream());
	}

	/**
	 * 导出
	 * @param request
	 * @param response
	 * @param dto
	 */
	@RequestMapping(value = "/reportApply", method = RequestMethod.GET)
	public void exportList(HttpServletRequest request,
						   HttpServletResponse response,
						   HpGoApplyDto dto) {
		response.setContentType(Servlets.EXCEL_TYPE);
		Servlets.setFileDownloadHeader(request, response, "申请报表.xlsx");
		try {
			OutputStream outputStream = response.getOutputStream();
			//导出Excel
			int[] columnWidth = {
					3000, 3000, 3000, 6000, 6000, 3000, 6000, 5000, 6000,5000
			};
			String[][][] headNames = {{
					{"申请人工号"}, {"申请人"}, {"审核人"}, {"园区"}, {"区域"}, {"通行次数"}, {"申请理由"}, {"申请通行时间"}, {"创建时间"}, {"审核状态"}
			}};
			String[][] valueName = {
					{"proposerNo"}, {"proposer"}, {"auditor"}, {"garden"}, {"area"}, {"goCount"}, {"applyReason"}, {"dateDay"}, {"createTimeStr"},{"statusName"}
			};
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			List<HpGoApplyVo> list = iHpGoApplyService.getListReport(dto);
			for (HpGoApplyVo avo:list){
				String status = HpConstant.HP_GO_APPLY_PROPOSER_STATUS_MAP.get(avo.getStatus());
				avo.setStatusName(status);
				String dateString = formatter.format(avo.getCreateTime());
				avo.setCreateTimeStr(dateString);
			}
			XlsxExcelCreation<HpGoApplyVo> creation =
					new XlsxExcelCreation<>(list, outputStream, columnWidth, headNames, valueName);
			creation.createXlsx();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
